package com.itheima.xmy.Demo;


public class NumberPrintTest {
    public static void main(String[] args) throws InterruptedException {
        //生产者消费者模型,需要用到等待唤醒机制，创建锁对象进行调用唤醒等待方法
        Number1 num = new Number1();
        //判断数字的奇偶性

        new Thread(() -> {
            while (true) {
                num.run();
            }
        }, "奇数").start();

        new Thread(() -> {
            while (true) {
                num.run4();
            }
        }, "偶数").start();

    }
}

class Number1 implements Runnable {
    //创建线程1   是奇数
    int a = 1;

    @Override
    public synchronized void run() {

        if (a % 2 == 1 && a < 101) {
            System.out.println(Thread.currentThread().getName() + " " + a);
            a++;
            // 唤醒别人, 等待自己
            this.notifyAll();

        } else {

            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public synchronized void run4() {

        if (a % 2 == 0 && a < 101) {
            System.out.println(Thread.currentThread().getName() + " " + a);
            a++;
            this.notifyAll();

        } else {


            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (a == 101) {
            System.exit(0);
        }

    }

}


